package com.sb.hot.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘雄康
 * @version V1.0
 * @copyright 2020 CMB - Powered By 研发一部
 * @date 2021/01/29 22:56
 */
@RestController
@RequestMapping("index")
public class Index {

    @Value("${version}")
    String version;

    public String getVersion() {
        return version;
    }

    @GetMapping("/")
    public String index() {
        classLoader();
        return "11";
    }

    @GetMapping("/ve")
    public String ve() {
        classLoader();
        return version;
    }

    @PostMapping("/user/login")
    public Map<String, Object> login(@RequestParam("username") String username, @RequestParam("password") String password) {
        if ("yudaoyuanma".equals(username) && "123456".equals(password)) {
            Map<String, Object> tokenMap = new HashMap<>();
            tokenMap.put("userId", 111);
            tokenMap.put("token", "xxtoken001xx");
            return tokenMap;
        }
        throw new RuntimeException("小朋友，你的账号密码不正确哟！");
    }

    @GetMapping("/user/get-current")
    public Map<String, Object> getCurrentUser(@RequestHeader("Authorization2") String authorization, @RequestParam("full") boolean full) {
        // 测试
        if (authorization.contains("token001")) {
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("id", 1);
            // full 为 true 时，获得完整信息
            if (full) {
                userInfo.put("nickname", "刘雄康");
                userInfo.put("gender", 1);
            }
            return userInfo;
        }
        throw new RuntimeException("小朋友，你没有登录哟！");
    }

    private void classLoader() {
        ClassLoader classLoader = this.getClass().getClassLoader();
        List<String> dataList = new ArrayList<>();
        while (null != classLoader) {
            dataList.add(classLoader.toString());
            classLoader = classLoader.getParent();
        }
        dataList.add(" \r\n   ");

        classLoader = Thread.currentThread().getContextClassLoader();
        while (null != classLoader) {
            dataList.add(classLoader.toString());
            classLoader = classLoader.getParent();
        }
        System.out.println("--- classLoader:   " + dataList);
    }

}
